﻿#include "pch.h"
#include "MyTask.h"
#include "MyTask.g.cpp"

using namespace winrt::Windows::ApplicationModel::Background;
using namespace winrt::Windows::Networking::BackgroundTransfer;
using namespace winrt::Windows::UI::Notifications;

namespace winrt::RuntimeComponent1::implementation
{
    void MyTask::Run(winrt::Windows::ApplicationModel::Background::IBackgroundTaskInstance const& taskInstance)
    {
		auto details = taskInstance.TriggerDetails().as<BackgroundTransferCompletionGroupTriggerDetails>();
		for (auto download : details.Downloads())
		{
			UpdateNotification(download);
		}
		for (auto upload : details.Uploads())
		{
			UpdateNotification(upload);
		}
    }
	void MyTask::UpdateNotification(const DownloadOperation& download)
	{
		NotificationData data;
		if (download.Progress().TotalBytesToReceive > 0) {
			double percent = double(download.Progress().BytesReceived) / download.Progress().TotalBytesToReceive;
			data.Values().Insert(L"value", to_hstring(percent));
		}
		else {
			data.Values().Insert(L"value", L"indeterminate");
		}
		switch (download.Progress().Status)
		{
		case BackgroundTransferStatus::Canceled:
			data.Values().Insert(L"status", L"已被取消");
			break;
		case BackgroundTransferStatus::Completed: {
			data.Values().Insert(L"status", L"已完成");
			break;
		}
		case BackgroundTransferStatus::Error:
			data.Values().Insert(L"status", L"错误");
			break;
		case BackgroundTransferStatus::Idle:
			data.Values().Insert(L"status", L"空闲");
			break;
		case BackgroundTransferStatus::PausedByApplication:
		case BackgroundTransferStatus::PausedCostedNetwork:
		case BackgroundTransferStatus::PausedNoNetwork:
		case BackgroundTransferStatus::PausedRecoverableWebErrorStatus:
		case BackgroundTransferStatus::PausedSystemPolicy:
			data.Values().Insert(L"status", L"已暂停");
			break;
		case BackgroundTransferStatus::Running:
			data.Values().Insert(L"status", L"");
			break;
		default:
			break;
		}
		ToastNotificationManager
			::CreateToastNotifier()
			.Update(data, L"Tag", to_hstring(download.Guid()));
	}
	void MyTask::UpdateNotification(const UploadOperation& upload)
	{
		NotificationData data;
		double percent = double(upload.Progress().BytesSent) / upload.Progress().TotalBytesToSend;
		data.Values().Insert(L"value", to_hstring(percent));
		switch (upload.Progress().Status)
		{
		case BackgroundTransferStatus::Canceled:
			data.Values().Insert(L"status", L"已被取消");
			break;
		case BackgroundTransferStatus::Completed: {
			data.Values().Insert(L"status", L"已完成");
			break;
		}
		case BackgroundTransferStatus::Error:
			data.Values().Insert(L"status", L"错误");
			break;
		case BackgroundTransferStatus::Idle:
			data.Values().Insert(L"status", L"空闲");
			break;
		case BackgroundTransferStatus::PausedByApplication:
		case BackgroundTransferStatus::PausedCostedNetwork:
		case BackgroundTransferStatus::PausedNoNetwork:
		case BackgroundTransferStatus::PausedRecoverableWebErrorStatus:
		case BackgroundTransferStatus::PausedSystemPolicy:
			data.Values().Insert(L"status", L"已暂停");
			break;
		case BackgroundTransferStatus::Running:
			data.Values().Insert(L"status", L"");
			break;
		default:
			break;
		}
		ToastNotificationManager
			::CreateToastNotifier()
			.Update(data, L"Tag", to_hstring(upload.Guid()));
	}
}
